<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改头像</title>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1">
    <link rel="shortcut icon" href="/static/favicon.ico">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="stylesheet" href="/static/css/sm.min.css">
    <link rel="stylesheet" href="/static/css/sm-extend.min.css">
</head>
<body>

<header class="bar bar-nav">
    <a class="icon icon-left pull-left" href="/page/setting"></a>
    <a class="icon icon-refresh pull-right"></a>
    <h1 class="title">修改头像</h1>
</header>
<div class="content">
    <div class="content-block-title">用户头像</div>
    <input type="hidden" id="mytitle" name="url"/>
    <input type="hidden" name="userId" id="userId">
    <div class="card demo-card-header-pic">
        <div valign="bottom" class="card-header color-white no-border no-padding" id="img">
            <!--todo-->
        </div>
        <div class="card-footer">
            <div id="uploader" class="wu-example">
                <!--用来存放文件信息-->
                <div id="thelist" class="uploader-list"></div>
                <div id="jindutiao" class="layui-progress layui-progress-big" lay-showpercent="true" lay-filter="demo"
                     style="width: 50%">
                    <div class="layui-progress-bar layui-bg-red" lay-percent="0%"></div>
                </div>
                <div class="btns">
                    <div id="picker">选择文件</div>
                    <button id="ctlBtn" class="btn btn-default">开始上传</button>
                </div>
                <div class="btns">
                    <button id="update" class="btn btn-default" onclick="updateImg()">更新头像</button>
                </div>
            </div>
        </div>
    </div>
</div>

<link rel="stylesheet" href="/static/webuploader/webuploader.css">
<script src="/static/jQuery/jquery.js"></script>
<script src="/static/webuploader/webuploader.js"></script>


<script>
    var element = null;

    var uploader = WebUploader.create({
        // swf文件路径
        swf: '/static/webuploader/Uploader.swf',
        // 文件接收服务端。
        server: 'http://localhost:8082/pic/upload',
        // 选择文件的按钮。可选。
        // 内部根据当前运行是创建，可能是input元素，也可能是flash.
        pick: '#picker',
        //分块
        chunked: true,
        auto: false,
        disableGlobalDnd: true,
        chunkSize: 1024 * 1024,//1M 为一分片
        fileNumLimit: 1024,
        fileSizeLimit: 1024 * 1024 * 1024,    // 200 M
        fileSingleSizeLimit: 1024 * 1024 * 1024,    // 50 M
        // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
        resize: false
    });

    var $list = $("#thelist");
    var $btn = $('#ctlBtn');

    // 当有文件被添加进队列的时候
    uploader.on('fileQueued', function (file) {
        $list.append('<div id="' + file.id + '" class="item">' +
            '<h4 class="info">' + file.name + '</h4>' +
            '<p class="state">等待上传...</p>' +
            '</div>');
    });

    //开始上传
    $btn.on('click', function () {
        uploader.upload();
    });

    uploader.on('uploadSuccess', function (file, object) {
        $('#' + file.id).find('p.state').text('已上传');
        var url = "";
        for (i in object) {
            url = object[i];
        }
        console.log("已上传:" + url);

        var o = JSON.parse(url);
        console.log(o.data);
        $("#mytitle").append(o.data);
    });
    uploader.on('uploadError', function (file) {
        $('#' + file.id).find('p.state').text('上传出错');
    });
    uploader.on('uploadComplete', function (file) {
        console.log("上传完成");
    });

    //进度条相关
    $(document).ready(function () {
        $("#jindutiao").hide();
    });


    $(function () {
        // 
        $.ajax({
            type: "post",
            url: "/sys/user/getLoginInfo",
            dataType: "json",
            // data :{
            //     "url" : imgUrl
            // },
            success: function (re) {
                var result = re;
                if (result.status == 0) {
                    $("#img").append('<img src=" ' + result.data.img + ' " width="100%" height="50%"/>');
                    $("#userId").append(result.data.userId);
                    // alert(result.data.img)
                } else {
                    window.location.href = '/page/loginUI'
                }
            }
        })
    });

    //更新头像
    function updateImg() {
        var imgUrl = $('#mytitle').text();
        // var userId = $('#userId').val();
        var userId = $('#userId').text();
        alert(imgUrl);
        alert(userId);
        // alert(userId1);
        $.ajax({
            type: "post",
            url: "/sys/user/updateImg",
            dataType: "json",
            data : {
                "userId":userId,
                "imgUrl" : imgUrl
            },
            success : function (re) {
                var result = re;
                if (result.status === 0){
                    alert(result.data)
                } else {
                    alert(result.msg)
                }
            }
        })
    }
</script>
</body>
</html>